package DesignModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tiang on 2018/7/10.
 * 命令模式
 */
public class CommandModel {
    /**
     * 实际的命令执行者
     */
    static class Barbecue{
        public void bakeMutton(){
            System.out.println("烤羊肉");
        }

        public void bakeChicken(){
            System.out.println("烤鸡");
        }
    }

    /**
     * 命令抽象类，持有执行者对象，可以切换不同的执行者
     */
    abstract static class Command{
        protected Barbecue bar;
        public Command(Barbecue b){
            bar = b;
        }
        abstract void execute();
    }

    /**
     * 具体的命令，调用执行者不同的方法
     */
    static class BakeMutton extends Command{

        public BakeMutton(Barbecue b) {
            super(b);
        }

        @Override
        void execute() {
            bar.bakeMutton();
        }
    }

    static class BakeChicken extends Command{

        public BakeChicken(Barbecue b) {
            super(b);
        }

        @Override
        void execute() {
            bar.bakeChicken();
        }
    }

    /**
     * 请求者，用来调用各种命令，可以实现命令的自由组合，可以在命令执行前后增加操作
     */
    static class Waiter{
        private List<Command> list = new ArrayList<>();

        public void addOrder(Command c){
            list.add(c);
        }

        public void execute(){
            list.forEach(c -> c.execute());
        }
    }

    public static void main(String[] args) {
        Barbecue barbecue = new Barbecue();
        Command mutton = new BakeMutton(barbecue),
                chicken = new BakeChicken(barbecue);
        Waiter waiter = new Waiter();
        waiter.addOrder(mutton);
        waiter.addOrder(mutton);
        waiter.addOrder(chicken);
        waiter.execute();
    }
}
